// pages/login/login.js
import {
  fetchCaptchaApi,
  loginApi
} from '../../utils/api.js';
import {
  randomString
} from "../../util.js";
import Toast from '@vant/weapp/toast/toast';
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    appName: app.globalData.appName,
    username: "",
    password: "",
    passwordType: "password",
    eyeType: "icon-eye-close",
    captchaCode: "",
    captchaKey: "",
    captchaBase64: "",
    captchaFocus: false,
    name: '',
  },

  formSubmit: function (e) {
    let name = e.detail.value.name
    this.setData({
      captchaCode: name
    })
    let that = this;
    loginApi({
      captcha: that.data.captchaCode,
      checkKey: that.data.captchaKey,
      username: that.data.username,
      password: that.data.password
    }, res => {
      let {
        success
      } = res;
      if (!success) {
        that.loadCaptcha()
        Toast({
          type: 'fail',
          message: res.message,
          onClose: () => {
            that.setData({
              captchaCode: '',
              captchaFocus: true
            })
          },
        });
        return;
      }
      let {
        userInfo
      } = res.result;
      app.globalData.token = res.result.token;
      app.globalData.isLogin = true;
      app.globalData.userInfo = userInfo;
      app.globalData.logiRole = res.result.logiRole
      wx.switchTab({
        url: '/pages/shipment/list/list'
      });
    });

  },
  changeEyeType() {
    if (this.data.eyeType === "icon-eye-close") {
      this.setData({
        eyeType: "icon-eye-open",
        passwordType: "text"
      })
    } else {
      this.setData({
        eyeType: "icon-eye-close",
        passwordType: "password"
      })
    }
  },
  // login() {
  //   let that = this;
  //   loginApi({
  //     captcha: that.data.captchaCode,
  //     checkKey: that.data.captchaKey,
  //     username: that.data.username,
  //     password: that.data.password
  //   }, res => {
  //     let {
  //       success
  //     } = res;
  //     if (!success) {
  //       that.loadCaptcha()
  //       Toast({
  //         type: 'fail',
  //         message: res.message,
  //         onClose: () => {
  //           that.setData({
  //             captchaCode: '',
  //             captchaFocus: true
  //           })
  //         },
  //       });
  //       return;
  //     }
  //     let {
  //       userInfo
  //     } = res.result;
  //     app.globalData.token = res.result.token;
  //     app.globalData.isLogin = true;
  //     app.globalData.userInfo = userInfo;
  //     app.globalData.logiRole = res.result.logiRole
  //     wx.switchTab({
  //       url: '/pages/shipment/list/list'
  //     });
  //   });
  // },
  bindUsername(e) {
    this.setData({
      username: e.detail.value
    })
  },
  bindPassworde(e) {
    this.setData({
      password: e.detail.value
    })
  },
  // bindCpatchaCode(e) {
  //   this.setData({
  //     captchaCode: e.detail.value
  //   })
  //   // let pwd = e.detail.value
  //   // return pwd.replace(/[^\w\.\/]/ig, '')
  // },
  loadCaptcha() {
    var that = this;
    var captchaKey = randomString(6);
    fetchCaptchaApi(captchaKey, res => {
      that.setData({
        captchaKey: captchaKey,
        captchaBase64: res.result
      });
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    wx.hideHomeButton();
    this.loadCaptcha();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})